package br.com.playonline.util.bolao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final DateUtil INSTANCE = new DateUtil();
	
	private DateUtil() {}
	
	public static DateUtil getInstance(){
		return INSTANCE;
	}
	
	public String dateToStrings(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		if(date == null){
			return "";
		}
		return dateFormat.format(date);
	}

	
	public String dateToStringsComHoras(Date date){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		if(date == null){
			return "";
		}
		return dateFormat.format(date);
	}

	public Date stringToDate(String data){
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Date date = null;
		try {
			date = dateFormat.parse(data);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public Integer mesesEntre(Date  dtInicial , Date dtFim ){
		Calendar inicial = Calendar.getInstance(); 
		Calendar fim = Calendar.getInstance();
		inicial.setTime(dtInicial);
		fim.setTime(dtFim);
		System.out.println("Meses Inicial: "+(inicial.get(Calendar.MONTH)+1));
		System.out.println("Meses final: "+fim.get(Calendar.MONTH));
	    int qtdMesesIni = (inicial.get(Calendar.YEAR) * 12) + (inicial.get(Calendar.MONTH)+1);  
	    int qtdMesesFim = (fim.get(Calendar.YEAR) * 12) + fim.get(Calendar.MONTH);  
	    
	    System.out.println("qtdMesesIni: "+ (fim.get(Calendar.YEAR) - (inicial.get(Calendar.YEAR))));
	    System.out.println("qtdMesesFim: "+fim.get(Calendar.YEAR));
	    
	    return qtdMesesFim - qtdMesesIni;  
	}  
	
	public void obtemQuantidadeDeMeses(Date data){
		
	}
	
	public static void main(String[] args) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		String dataInicical = "23/12/1984";
		Date dtInicial = dateFormat.parse(dataInicical);
		Date dtFinal = new Date();
		System.out.println("Idade: "+DateUtil.getInstance().mesesEntre(dtInicial, dtFinal)/12);
	}
	
}
